package com.ruoyi.common.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;

public class RedisMonitorUtil {

	protected static Log logger = LogFactory.getLog(RedisMonitorUtil.class);

	public static class RedisInfo {

		private boolean success = true;

		private String usedMemory;

		private long connectedClients;

		private long totalCommandsProcessed;

		private long keys;

		private long expires;

		private String error;

		public String getUsedMemory() {
			return usedMemory;
		}

		public void setUsedMemory(String usedMemory) {
			this.usedMemory = usedMemory;
		}

		public long getConnectedClients() {
			return connectedClients;
		}

		public void setConnectedClients(long connectedClients) {
			this.connectedClients = connectedClients;
		}

		public long getTotalCommandsProcessed() {
			return totalCommandsProcessed;
		}

		public void setTotalCommandsProcessed(long totalCommandsProcessed) {
			this.totalCommandsProcessed = totalCommandsProcessed;
		}

		public long getKeys() {
			return keys;
		}

		public void setKeys(long keys) {
			this.keys = keys;
		}

		public long getExpires() {
			return expires;
		}

		public void setExpires(long expires) {
			this.expires = expires;
		}

		public boolean isSuccess() {
			return success;
		}

		public void setSuccess(boolean success) {
			this.success = success;
		}

		public String getError() {
			return error;
		}

		public void setError(String error) {
			this.error = error;
		}

	}

	public static RedisInfo get(String host, int port, String password) {

		Jedis jedis = null;

		RedisInfo info = null;
		try {
			jedis = new Jedis(host, port);
			if (!StringUtils.isEmpty(password)) {
				jedis.auth(password);
			}
			String s = jedis.info();
			info = parse(s);
		} catch (Throwable t) {
			info = new RedisInfo();
			info.setSuccess(false);
			info.setError(t.getMessage());
			logger.warn(t.getMessage(), t);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}

		return info;

	}

	private static RedisInfo parse(String s) {
		RedisInfo info = new RedisInfo();

		String[] lines = s.split("\n");
		for (String line : lines) {
			line = line.trim();
			if (StringUtils.isEmpty(line) || line.startsWith("#")) {
				continue;
			}
			String[] datas = line.split(":");
			if (datas.length != 2) {
				continue;
			}

			String key = datas[0];
			switch (key) {
			case "used_memory_human":
				info.setUsedMemory(datas[1]);
				break;
			case "connected_clients":
				info.setConnectedClients(Long.parseLong(datas[1]));
				break;
			case "total_commands_processed":
				info.setTotalCommandsProcessed(Long.parseLong(datas[1]));
				break;
			case "db0":
				String[] dbInfos = datas[1].split(",");
				for (String dbInfo : dbInfos) {
					String[] dbInfoDatas = dbInfo.split("=");
					String dbInfoKey = dbInfoDatas[0];
					if (StringUtils.equalsIgnoreCase("keys", dbInfoKey)) {
						info.setKeys(Long.parseLong(dbInfoDatas[1]));
					} else if (StringUtils.equalsIgnoreCase("expires", dbInfoKey)) {
						info.setExpires(Long.parseLong(dbInfoDatas[1]));
					}
				}
				break;
			default:
				break;
			}

		}

		return info;
	}

	public static void main(String[] args) {
		RedisInfo info = get("192.168.110.134", 6400, "98de599279ca40e69095e0e84822659b");
		System.out.println(JSON.toJSON(info));
	}

}
